package com.lanou.tipsy.adapter.playerLists;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.tipsy.R;
import com.lanou.tipsy.data.MyEventBus;
import com.lanou.tipsy.entity.playerList.local.Mp3Info;
import com.lanou.tipsy.entity.playerList.personal.PersonalList;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by FengJun on 15/10/30.
 * 本地歌曲页面popupWindow弹出的播放列表--适配器
 */
public class LocalListAdapter extends BaseAdapter {

    private Context context; //上下文对象引用
    public List<Mp3Info> mp3Infos; //存放Mp3Info引用的集合
    private Mp3Info mp3Info; //Mp3Info对象引用
    private ViewContainer vc;
    private int position;//当前正在播放的位置
    private ArrayList<PersonalList> datas;//自己的播放歌曲实体类集合

    public ArrayList<PersonalList> getDatas() {
        return datas;
    }

    private void getMusicInfo() {
        for (Mp3Info info : mp3Infos) {
            PersonalList personalList = new PersonalList();
            personalList.setPicture(null);
            personalList.setSongTitle(info.getTitle());
            personalList.setTag("本地歌曲");
            personalList.setDuration(info.getDuration());
            personalList.setSongsUrl(info.getUrl());
            personalList.setSingerName(info.getArtist());
            personalList.setId(info.getId());
            personalList.setAlbum_id(info.getAlbum_id());
            datas.add(personalList);
        }
    }

    public List<Mp3Info> getMp3Infos() {
        return mp3Infos;
    }

    public LocalListAdapter(Context context, List<Mp3Info> mp3Infos) {
        this.context = context;
        this.mp3Infos = mp3Infos;
        datas = new ArrayList<>();
        getMusicInfo();
        EventBus.getDefault().register(this);
    }

    public LocalListAdapter(Context context, List<Mp3Info> mp3Infos, int position) {
        this.context = context;
        this.mp3Infos = mp3Infos;
        this.position = position;
        EventBus.getDefault().register(this);
    }

    public void onEvent(MyEventBus.ListPosition index) {
        this.position = index.getIndex();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mp3Infos.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        vc = null;
        if (convertView == null) {
            vc = new ViewContainer();
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.item_local_music_list, null);
            vc.music_title = (TextView) convertView.findViewById(R.id.music_title);
            vc.music_artist = (TextView) convertView.findViewById(R.id.music_artist);
            vc.music_duration = (TextView) convertView.findViewById(R.id.music_duration);
            vc.music_position = (TextView) convertView.findViewById(R.id.tv_local_position);
            convertView.setTag(vc);
        } else {
            vc = (ViewContainer) convertView.getTag();
        }
        mp3Info = mp3Infos.get(position);
        if (position + 1 < 10) {
            vc.music_position.setText("0" + (position + 1));
        } else {
            vc.music_position.setText("" + (position + 1));
        }
        if (this.position == position) {
            vc.music_artist.setTextColor(context.getResources().getColor(R.color.colorAccent));
            vc.music_title.setTextColor(context.getResources().getColor(R.color.colorAccent));
        } else {
            vc.music_title.setTextColor(context.getResources().getColor(R.color.color_black_tint));
            vc.music_artist.setTextColor(context.getResources().getColor(R.color.color_grey));
        }
        vc.music_title.setText(mp3Info.getTitle());         //显示标题
        vc.music_artist.setText(mp3Info.getArtist());       //显示艺术家
        vc.music_duration.setText(String.valueOf(formatTime(mp3Info.getDuration()))); //显示长度
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return mp3Infos.get(position);
    }

    //将歌曲的时间转换为分秒的制度
    public static String formatTime(Long time) {
        String min = time / (1000 * 60) + "";
        String sec = time % (1000 * 60) + "";

        if (min.length() < 2)
            min = "0" + min;
        switch (sec.length()) {
            case 4:
                sec = "0" + sec;
                break;
            case 3:
                sec = "00" + sec;
                break;
            case 2:
                sec = "000" + sec;
                break;
            case 1:
                sec = "0000" + sec;
                break;
        }
        return min + ":" + sec.trim().substring(0, 2);
    }
}
